package com.only4play.importapt.web.resources;


import com.only4play.common.model.GeneralResponse;
import com.only4play.importapt.model.entity.CustomerEntity;
import com.only4play.importapt.service.AbstractMergeRequestService;
import com.only4play.importapt.service.CustomerService;
import com.only4play.importapt.service.impl.CustomerWrapBatchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(path = "/api/v1/customers")
public class CustomerResource {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerWrapBatchServiceImpl wrapBatchService;

    @GetMapping("/qryById")
    public GeneralResponse<CustomerEntity> qryById(
        @RequestParam("id") String id
    ) {
        CustomerEntity entity = customerService.getById(id);
        return GeneralResponse.of(entity);
    }

    @GetMapping("/merge/qryById")
    public GeneralResponse<CustomerEntity> mergeQryById(
        @RequestParam("id") String id
    ) throws Exception {
        AbstractMergeRequestService.WrapRequest request = new AbstractMergeRequestService.WrapRequest(
            UUID.randomUUID().toString().replaceAll("-", ""),
            id,
            new CompletableFuture<>()
        );
        Object query = wrapBatchService.query(request);
        return GeneralResponse.of((CustomerEntity) query);
    }
}
